﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using BLL;
using Utils;

namespace UI.ModEmployee
{
    public partial class EmpRewardsList : System.Web.UI.Page
    {
        EmpRewardsBiz empRewardsBiz = new EmpRewardsBiz();
     
        string empCode = "";
        string bTm = "";
        string eTm = "";
        string dID = "";//部门名称
        string typeID = "";
        string account = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (Request.Params["EmpCode"] != null) empCode = Request.Params["EmpCode"].ToString().Trim();
            if (Request.Params["BTime"] != null) bTm = Request.Params["BTime"].ToString().Trim();
            if (Request.Params["ETime"] != null) eTm = Request.Params["ETime"].ToString().Trim();
            if (Request.Params["DeptID"] != null) dID = Request.Params["DeptID"].ToString().Trim();
            if (Request.Params["TypeID"] != null) typeID = Request.Params["TypeID"].ToString().Trim();
            if (!IsPostBack)
            {
                if (dID != "" && dID != "0")
                    new DepartmentBiz().InitDropDownList(ddlDeptID, dID);
                else
                    new DepartmentBiz().InitDropDownList(ddlDeptID, "0");
                if (typeID != "" && typeID != "0")
                    new DefDicBiz().InitDropDownList("Admin_Rewards_type", ddlRewardsTypeName, typeID);
                else
                    new DefDicBiz().InitDropDownList("Admin_Rewards_type", ddlRewardsTypeName, "");
                ddlRewardsTypeName.Items.Add(new ListItem("全部奖励", "30000"));
                ddlRewardsTypeName.Items.Add(new ListItem("全部惩罚", "30001"));
                btnAdd.Attributes.Add("onclick", "javascript:ShowLink('EmpRewardsAdd.aspx',800,550);");

                if (empCode != "")
                {
                    txtSerWord.Text = empCode;
                    if (bTm != "") txtBtime.Text = bTm;
                    if (eTm != "") txtEtime.Text = eTm;
                    BondGvList(dID, empCode, bTm, eTm, typeID);
                }
                else//显示最近
                {
                    BondGvList("", "", System.DateTime.Now.AddDays(-10).ToString("yyyy-MM-dd"), System.DateTime.Now.ToString("yyyy-MM-dd"), "");
                }
            }
        }
        protected void btnEmSearch_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpRewardsList.aspx", RightChar.Ser))
            {
                JSUtility.Alert(this, "无权限！");
                return;
            }
            string deptID = ddlDeptID.SelectedItem.Text.Trim();
            if (deptID == "请选择部门") deptID = "";
            string serWord = txtSerWord.Text.Trim();
            string bTime = txtBtime.Text.Trim();
            string eTime = txtEtime.Text.Trim();
            string typeID = ddlRewardsTypeName.SelectedValue;
       
            if (serWord == "" && bTime == "" && eTime == "" && deptID == "" && typeID == "0")
            {
                bTime = System.DateTime.Now.AddMonths(-2).ToString("yyyy-MM-dd");
                eTime = System.DateTime.Now.ToString("yyyy-MM-dd");
            }
            if (StringUtility.GetDiffDays(bTime, eTime) > XmlUtility.GetNumFormConfig("daysOfSearch"))
            {
                JSUtility.Alert(this, "查询日期间隔不能大于" + XmlUtility.GetNumFormConfig("daysOfSearch") + "天！");
                return;
            }
            BondGvList(deptID, serWord, bTime, eTime, typeID);


        }
        protected void BondGvList(string deptID, string serWord, string bTime, string eTime, string typeID)
        {
            DataTable dt = empRewardsBiz.GetLists(deptID, serWord, bTime, eTime, typeID);
            if (dt.Rows.Count > 0)
            {
                lbAmount.Text = "合计:    " + dt.Rows.Count; }
            else
            {
                lbAmount.Text = "";
            }
                gvList.DataSource = dt;
                gvList.DataBind();
           
        }


      
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpRewardsList.aspx", RightChar.Out))
            {
                JSUtility.Alert(this, "无权限！");
                return;
            }
            //ExcelUtility.GridViewToExcel(gvList, "EmpRewards.xls");
            string deptID = ddlDeptID.SelectedItem.Text.Trim();
            if (deptID == "请选择部门") deptID = "";
            string serWord = txtSerWord.Text.Trim();
            string bTime = txtBtime.Text.Trim();
            string eTime = txtEtime.Text.Trim();
            string typeID = ddlRewardsTypeName.SelectedValue;
            ExcelUtility.ExportDsToXls(this, empRewardsBiz.GetLists(deptID, serWord, bTime, eTime, typeID, "Export"), "EmpRewards.xls");
        }
       

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Visible = false;
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#F3F8F8'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
            }
        }
    }
}